package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.service.CheckItemService;
import com.itheima.service.MemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('MEMBER_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('MEMBER_ADD')")
    public Result add(@RequestBody Member member){
        try {
            memberService.add(member);
            return new Result(true,"添加会员成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加会员失败");
        }
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('MEMBER_EDIT')")
    public Result update(@RequestBody Member member){
        try {
            memberService.update(member);
            return new Result(true,"修改会员成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改会员失败");
        }
    }
    @GetMapping("/toUpdate")
    @PreAuthorize("hasAuthority('MEMBER_EDIT')")
    public Result toUpdate(Integer id){
        try {
            Member member = memberService.findById(id);
            return new Result(true,"查询会员成功",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询会员失败");
        }
    }
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('MEMBER_DELETE')")
    public Result delete(Integer id){
        try {
            memberService.delete(id);
            return new Result(true,"删除会员成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除会员失败");
        }
    }

}
